//
//  ChufangViewController.m
//  TY-C
//
//  Created by xyyl on 2024/3/4.
//

#import "ChufangViewController.h"
#import "ChufangTableViewCell.h"
#import "Chufang_AddViewController.h"
#import "Chufang_EditViewController.h"
#import "Pac_GetLocalCfView.h"
#import "TY_DataCommand.h"

@interface ChufangViewController ()<UITableViewDelegate,UITableViewDataSource>
///
@property (nonatomic,strong) UILabel * titleLab;
///
@property (nonatomic,strong) UITextField * cfTxt;
///
@property (nonatomic,strong) UIButton * searchBtn;
///
@property (nonatomic,strong) UITableView * tableView;
///
@property (nonatomic,strong) UIButton * xjcfBtn;
///
@property (nonatomic,strong) UIButton * dqcfBtn;
///
@property (nonatomic,strong) UIButton * scBtn;
///
@property (nonatomic,strong) NSMutableArray * dataArray;


@end

@implementation ChufangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [[NSMutableArray alloc]initWithArray:[TY_DataCommand checkAllData]];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshView) name:@"chufangRefresh" object:nil];
    [self initTopView];
    [self initTabelView];
    [self initBottomViews];
    

    
}

- (void)refreshView {
    
    if (_dataArray) {
        _dataArray = [[NSMutableArray alloc]initWithArray:[TY_DataCommand checkAllData]];
        [_tableView reloadData];

    }
}

- (void)initBottomViews {
    _xjcfBtn = [UIButton new];
    [self.view addSubview:_xjcfBtn];
    [_xjcfBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tableView.mas_bottom).offset(5*IPADSCALE);
        make.left.equalTo(_tableView);
        make.width.equalTo(@(65*IPADSCALE));
        make.height.equalTo(@(22*IPADSCALE));
    }];
    [_xjcfBtn setTitle:@"新建处方" forState:0];
    [_xjcfBtn setTitleColor:RGBColor(13, 157, 219) forState:0];
    _xjcfBtn.titleLabel.font = ZYQFONT(16);
    _xjcfBtn.clipsToBounds = YES;
    _xjcfBtn.layer.borderWidth = 1;
    _xjcfBtn.layer.cornerRadius = 2;
    _xjcfBtn.layer.borderColor = RGBColor(13, 157, 219).CGColor;
    
    
    _dqcfBtn = [UIButton new];
    [self.view addSubview:_dqcfBtn];
    [_dqcfBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(_xjcfBtn);
        make.left.equalTo(_xjcfBtn.mas_right).offset(10 *IPADSCALE);
        
    }];
    [_dqcfBtn setTitle:@"读取处方" forState:0];
    [_dqcfBtn setTitleColor:RGBColor(13, 157, 219) forState:0];
    _dqcfBtn.titleLabel.font = ZYQFONT(16);
    _dqcfBtn.clipsToBounds = YES;
    _dqcfBtn.layer.borderWidth = 1;
    _dqcfBtn.layer.cornerRadius = 2;
    _dqcfBtn.layer.borderColor = RGBColor(13, 157, 219).CGColor;
    
    
    _scBtn = [UIButton new];
    [self.view addSubview:_scBtn];
    [_scBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(_xjcfBtn);
        make.left.equalTo(_dqcfBtn.mas_right).offset(10 *IPADSCALE);
    }];
    [_scBtn setTitle:@"删除" forState:0];
    [_scBtn setTitleColor:RGBColor(239, 104, 30) forState:0];
    _scBtn.titleLabel.font = ZYQFONT(16);
    _scBtn.clipsToBounds = YES;
    _scBtn.layer.borderWidth = 1;
    _scBtn.layer.cornerRadius = 2;
    _scBtn.layer.borderColor = RGBColor(239, 104, 30).CGColor;
    
    
    _xjcfBtn.tag = 0;
    _dqcfBtn.tag = 1;
    _scBtn.tag = 2;
    
    [_xjcfBtn addTarget:self action:@selector(bottomsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_dqcfBtn addTarget:self action:@selector(bottomsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_scBtn addTarget:self action:@selector(bottomsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)bottomsBtnAction:(UIButton *)btn {
    btn.userInteractionEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        btn.userInteractionEnabled =  YES;
    });
    
    
    NSInteger tag = btn.tag;
    if(tag == 0){
        ///新建处方
        Chufang_AddViewController * cfaddVC = [[Chufang_AddViewController alloc]init];
        [self addChildViewController:cfaddVC];
        
        [self.view addSubview:cfaddVC.view];
    }
    if(tag == 1){
        ///读取处方
        Pac_GetLocalCfView * ppp = [[Pac_GetLocalCfView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [ppp sureBtnwithDoneBlock:^(Pac_GetLocalCfView * _Nonnull pickerView, NSInteger selectedIndex, id  _Nonnull selectedValue) {
            
        } CancelBlock:^(Pac_GetLocalCfView * _Nonnull pickerView) {
            
        }];
    }
    if(tag == 2){
        ///删除
        
        NSMutableArray * strArr = [NSMutableArray new];
        for (People * model in self.dataArray) {
            if (model.isSel == YES) {
                [strArr addObject:model.creatTime];
            }
        }
        if (strArr.count > 0) {
            for (NSString * ss in strArr) {
                [TY_DataCommand delDataWithCreateID:ss];
            }
            
            [[NSNotificationCenter defaultCenter] postNotificationName:ChuFangRefresh object:nil];

        }
        
        return;
        

    }
    
}

- (void)initTopView {
    _titleLab = [UILabel new];
    [self.view addSubview:_titleLab];
    _titleLab.frame = CGRectMake(25 * IPADSCALE, 20 * IPADSCALE, 200, 18 * IPADSCALE);
    _titleLab.text = @"处方管理";
    _titleLab.font = [UIFont boldSystemFontOfSize:20];
    _titleLab.textColor = [UIColor blackColor];
    
    _cfTxt = [UITextField new];
    [self.view addSubview:_cfTxt];
    _cfTxt.frame = CGRectMake(CGRectGetMinX(_titleLab.frame), CGRectGetMaxY(_titleLab.frame)+16, 200 * IPADSCALE, 22 * IPADSCALE);
    _cfTxt.placeholder = @"请输入处方名称";
    _cfTxt.backgroundColor = [UIColor whiteColor];
    _cfTxt.clipsToBounds = YES;
    _cfTxt.layer.borderWidth = 1;
    _cfTxt.layer.borderColor = RGBColor(176, 176, 176).CGColor;
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, CGRectGetHeight(_cfTxt.frame))];
    imageView.image = [UIImage imageNamed:@"img_none"];
    UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, CGRectGetHeight(_cfTxt.frame))];
    [leftView addSubview:imageView];
    _cfTxt.leftView = imageView;
    _cfTxt.leftViewMode = UITextFieldViewModeAlways;
    
    _searchBtn = [UIButton new];
    [self.view addSubview:_searchBtn];
    _searchBtn.frame = CGRectMake(CGRectGetMaxX(_cfTxt.frame), CGRectGetMinY(_cfTxt.frame), 50*IPADSCALE, CGRectGetHeight(_cfTxt.frame));
    [_searchBtn setBackgroundColor:RGBColor(13, 157, 219)];
    [_searchBtn setTitle:@"搜索" forState:0];
    

}

- (UIView *)pinitTableViewHeader {
    UIView * vvv = [UIView new];
    
    CGFloat www = CGRectGetWidth(_tableView.frame);
    vvv.frame = CGRectMake(0, 0, www, 18*IPADSCALE);
    vvv.backgroundColor = RGBColor(241, 249, 253);
    
    NSArray * aaa =
    @[@"处方名称",@"处方类型",@"频率",@"脉宽",@"治疗时间",@"持续",@"间隔",@"上升",@"下降",@"延迟",@"发送参数",@"编辑",@"删除"];
    CGFloat space = 5 *IPADSCALE;
    CGFloat viewWWW = (www - 20 * IPADSCALE - space *13)/30;
    CGFloat oriX = 20 * IPADSCALE;
    for (int i = 0; i <13 ; i++) {
        UILabel * ll = [UILabel new];
        [vvv addSubview:ll];
        if(i == 0||i == 1||i == 4||i == 10) {
            ll.frame = CGRectMake(oriX, 0, viewWWW * 3, CGRectGetHeight(vvv.frame));

        }else{
            ll.frame = CGRectMake(oriX, 0, viewWWW * 2, CGRectGetHeight(vvv.frame));

        }
        ll.text = aaa[i];
        ll.textAlignment = NSTextAlignmentCenter;
//        ll.backgroundColor = [UIColor yellowColor];
        ll.font = ZYQFONT(18);
        ll.textColor = RGBColor(102, 102, 102);
        oriX = CGRectGetMaxX(ll.frame) + space;
    }
    
    return vvv;
    
}

- (void)initTabelView {
    NSLog(@"%f",self.viewWidth);
    CGFloat widdd = self.viewWidth - 25 * 2 * IPADSCALE;
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_cfTxt.frame), CGRectGetMaxY(_cfTxt.frame)+10, widdd, 240*IPADSCALE) style:0];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle = 0;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.scrollIndicatorInsets = _tableView.contentInset;
    }
//    _tableView.tableHeaderView = [self pinitTableViewHeader];
    [self.view addSubview:_tableView];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [self pinitTableViewHeader];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 18*IPADSCALE;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChufangTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"id"];
    if (cell == nil) {
        cell = [[ChufangTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"];
        cell.cellWidth =CGRectGetWidth(_tableView.frame);
        [cell.bjBtn addTarget:self action:@selector(cellBjBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    cell.pModel = self.dataArray[indexPath.row];
    cell.bjBtn.tag = indexPath.row;

    return cell;
}

- (void)cellBjBtnAction:(UIButton *)btn {
    NSInteger ttt = btn.tag;
    People * pmodel =self.dataArray[ttt];
    
    Chufang_AddViewController * cfaddVC = [[Chufang_AddViewController alloc]init];
    cfaddVC.pModel = pmodel;
    [self addChildViewController:cfaddVC];
    [self.view addSubview:cfaddVC.view];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (27+4)*IPADSCALE;
}

@end
